package com.snowleopard.findpeople.web.search;

import org.apache.commons.lang.xwork.StringUtils;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springside.modules.orm.Page;

import com.opensymphony.xwork2.ActionSupport;
import com.snowleopard.findpeople.entity.publish.InfoPublish;
import com.snowleopard.findpeople.service.mgmt.ParameterManager;
import com.snowleopard.findpeople.service.publish.InfoPublishManager;

@Namespace("/search")
@Results({ @Result(name = "home", location = "home.jsp"),
        @Result(name = "view", location = "view.jsp") })
public class SearchAction extends ActionSupport {

    private static final long serialVersionUID = -6130652903717507173L;
    private static final String HOME = "home";
    private static final String VIEW = "view";

    @Autowired
    private InfoPublishManager InfoPublishManager;
    @Autowired
    private ParameterManager parameterManager;

    // --------- Parameters which return to parent page ---------STAR
    private String adjustAreaId;
    private String adjustTradeId;
    // --------- Parameters which return to parent page ---------END

    private String infoPublishId;
    private InfoPublish infoPublish;

    @Value("#{fpProperties['info.search.page.size']}")
    private String PAGE_SIZE;

    private Page<InfoPublish> page;
    private boolean clean;

    @Override
    public String execute() throws Exception {

        if (clean) {
            infoPublish = null;
            return HOME;
        }

        if (infoPublish == null) {
            return HOME;
        }

        if (page == null) {
            page = new Page<InfoPublish>(Integer.valueOf(PAGE_SIZE));
        } else {
            page.setPageSize(Integer.valueOf(PAGE_SIZE));
        }

        if (!page.isOrderBySetted()) {
            page.setOrderBy("id");
            page.setOrder(Page.ASC);
        }

        if (StringUtils.isNotBlank(adjustAreaId)) {
            infoPublish.getArea().setId(adjustAreaId);
        }

        if (StringUtils.isNotBlank(adjustTradeId)) {
            infoPublish.getTrade().setId(adjustTradeId);
        }

        page = InfoPublishManager.searchPublishInfoByCondition(page, infoPublish);
        return SUCCESS;
    }

    public String view() throws Exception {
        if (StringUtils.isNotBlank(infoPublishId)) {
            infoPublish = InfoPublishManager.getInfoPublish(infoPublishId);
            return VIEW;
        } else {
            return execute();
        }
    }

    public ParameterManager getParameterManager() {
        return parameterManager;
    }

    public void setParameterManager(ParameterManager parameterManager) {
        this.parameterManager = parameterManager;
    }

    public String getInfoPublishId() {
        return infoPublishId;
    }

    public void setInfoPublishId(String infoPublishId) {
        this.infoPublishId = infoPublishId;
    }

    public InfoPublish getInfoPublish() {
        return infoPublish;
    }

    public void setInfoPublish(InfoPublish infoPublish) {
        this.infoPublish = infoPublish;
    }

    public Page<InfoPublish> getPage() {
        return page;
    }

    public void setPage(Page<InfoPublish> page) {
        this.page = page;
    }

    public boolean isClean() {
        return clean;
    }

    public void setClean(boolean clean) {
        this.clean = clean;
    }

    public String getAdjustTradeId() {
        return adjustTradeId;
    }

    public void setAdjustTradeId(String adjustTradeId) {
        this.adjustTradeId = adjustTradeId;
    }

    public String getAdjustAreaId() {
        return adjustAreaId;
    }

    public void setAdjustAreaId(String adjustAreaId) {
        this.adjustAreaId = adjustAreaId;
    }
}
